package com.puman.beautyfarmcounselor.ui.schedule.adpter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.puman.beautyfarmcounselor.MainActivity;
import com.puman.beautyfarmcounselor.R;
import com.puman.beautyfarmcounselor.ui.schedule.modle.BookData;
import com.puman.beautyfarmcounselor.ui.schedule.modle.BookInfo;
import com.puman.beautyfarmcounselor.util.MethedUtil;

import java.util.ArrayList;
import java.util.List;

import static com.puman.beautyfarmcounselor.R.color.themeColor;

/**
 * Created by Curry on 2015/6/12.
 */
public class ScheduleBookCustomerAdapter extends RecyclerView.Adapter<ScheduleBookCustomerAdapter.ViewHolder> {

    private Context context;
    private BookData bookData;

    public ScheduleBookCustomerAdapter(Context context, BookData  bookData) {
        this.context = context;
        this.bookData = bookData;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final ImageView timeicon;
        private final ImageView doticon;
        private final TextView time;
        private final TextView customerName;
        private final TextView mobile;
        private final TextView employerInfo;
        private final TextView employer;
        private final TextView project;
        private final TextView status;
        private final LinearLayout bookLayout;
        private final TextView storeName;

        public ViewHolder(View v) {
            super(v);
            timeicon = (ImageView) v.findViewById(R.id.task_list_item_icon);
            doticon = (ImageView) v.findViewById(R.id.task_list_item_dot_icon);
            time = (TextView) v.findViewById(R.id.bookitem_booktime);
            customerName = (TextView) v.findViewById(R.id.customer_name);
            mobile = (TextView) v.findViewById(R.id.customer_mobile);
            employerInfo = (TextView) v.findViewById(R.id.employee_info);
            employer = (TextView) v.findViewById(R.id.employee_name);
            project = (TextView) v.findViewById(R.id.book_project);
            status = (TextView) v.findViewById(R.id.book_status);
            bookLayout = (LinearLayout) v.findViewById(R.id.book_item_layout);
            storeName = (TextView) v.findViewById(R.id.store_name);
        }

        public ImageView getTimeicon() {
            return timeicon;
        }

        public ImageView getDoticon() {
            return doticon;
        }

        public TextView getTime() {
            return time;
        }

        public TextView getCustomerName() {
            return customerName;
        }

        public TextView getMobile() {
            return mobile;
        }

        public TextView getEmployerInfo() {
            return employerInfo;
        }

        public TextView getEmployer() {
            return employer;
        }

        public TextView getProject() {
            return project;
        }

        public TextView getStatus() {
            return status;
        }

        public LinearLayout getBookLayout() {
            return bookLayout;
        }

        public TextView getStoreName() {
            return storeName;
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.bookcustomer_list_item, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        int color = 0;
        if (bookData.getData().get(position).getStatus() ==0 ) {
            color = context.getResources().getColor(R.color.schedule_uncomfired);
            holder.getTimeicon().setImageResource(R.drawable.book_customer_unconfirmed);
            holder.getDoticon().setImageResource(R.drawable.book_item_uncomfirmed_dot_icon);
//            holder.getStatus().setText("未确认");
        } else if (bookData.getData().get(position).getStatus()==1) {
            color = context.getResources().getColor(R.color.schedule_comfired);
            holder.getTimeicon().setImageResource(R.drawable.book_customer_confirmed);
            holder.getDoticon().setImageResource(R.drawable.book_item_comfirmed_dot_icon);
//            holder.getStatus().setText("已确认");
        } else if (bookData.getData().get(position).getStatus()==2) {
            color = context.getResources().getColor(R.color.schedule_cancle);
            holder.getTimeicon().setImageResource(R.drawable.book_customer_cancle);
            holder.getDoticon().setImageResource(R.drawable.book_item_cancle_dot_icon);
//            holder.getStatus().setText("已取消");
        } else {
            color = context.getResources().getColor(R.color.schedule_cancle);
            holder.getTimeicon().setImageResource(R.drawable.book_customer_cancle);
            holder.getDoticon().setImageResource(R.drawable.book_item_cancle_dot_icon);
//            holder.getStatus().setText("已到店");
        }
        holder.getStatus().setText(bookData.getData().get(position).getStatus_des());
        holder.getTime().setTextColor(color);
        holder.getCustomerName().setTextColor(color);
        holder.getMobile().setTextColor(color);
        holder.getEmployer().setTextColor(color);
        holder.getEmployerInfo().setTextColor(color);
        holder.getProject().setTextColor(color);
        holder.getStatus().setTextColor(color);
        holder.getStoreName().setTextColor(color);

        holder.getTime().setText(bookData.getData().get(position).getTime());
        holder.getCustomerName().setText(bookData.getData().get(position).getCust_name());
        holder.getMobile().setText(bookData.getData().get(position).getCust_mobile());
        holder.getEmployer().setText(bookData.getData().get(position).getEmp_name());
        holder.getEmployer().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, bookData.getData().get(position).getEmp_name(), Toast.LENGTH_LONG).show();
            }
        });
        holder.getProject().setText(bookData.getData().get(position).getProduct_name());
        if (bookData.getData().get(position).getStore_name().length() > 4){
            holder.getStoreName().setText(bookData.getData().get(position).getStore_name().substring(0, 4)+"..");
        }else{
            holder.getStoreName().setText(bookData.getData().get(position).getStore_name());
        }
        holder.getStoreName().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, bookData.getData().get(position).getStore_name(), Toast.LENGTH_SHORT).show();
            }
        });

        holder.getBookLayout().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                reLogin();
//                MainActivity.getInstace().getWindow().setFlags(WindowManager.LayoutParams.ALPHA_CHANGED, WindowManager.LayoutParams.ALPHA_CHANGED);
            }
        });

        holder.project.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                MethedUtil.UtilToast(context, bookData.getData().get(position).getProduct_name());
                Toast.makeText(context, bookData.getData().get(position).getProduct_name(), Toast.LENGTH_LONG).show();
            }
        });
        holder.getMobile().setTag(position);
        holder.getMobile().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = (int) v.getTag();
                showNoticy(position);
            }
        });

    }

    @Override
    public int getItemCount() {
        return bookData.getData().size();
    }

    private AlertDialog alertDialog;

    public void reLogin() {
        alertDialog = new AlertDialog.Builder(context)
                .create();
        alertDialog.show();
        alertDialog.setCanceledOnTouchOutside(false);
        Window window = alertDialog.getWindow();
        window.setContentView(R.layout.book_operartion_dialog);
//        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        TextView sureBtn = (TextView) window.findViewById(R.id.sure_submit);
        final TextView callPhone = (TextView) window.findViewById(R.id.book_phone_btn);
        final TextView cancleBook = (TextView) window.findViewById(R.id.book_cancle_btn);
        final TextView sureBook = (TextView) window.findViewById(R.id.book_sure_btn);
        ImageView closeWin = (ImageView) window.findViewById(R.id.close_win);

        closeWin.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        sureBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });

        sureBook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sureBook.setTextColor(context.getResources().getColor(R.color.white));
                sureBook.setBackground(context.getResources().getDrawable(R.drawable.circle_white_bg_gray_bound_0stroke_select));
                callPhone.setTextColor(context.getResources().getColor(R.color.schedule_book_info_font));
                callPhone.setBackground(context.getResources().getDrawable(R.drawable.circle_white_bg_gray_bound_0stroke));
                cancleBook.setTextColor(context.getResources().getColor(R.color.schedule_book_info_font));
                cancleBook.setBackground(context.getResources().getDrawable(R.drawable.circle_white_bg_gray_bound_0stroke));
            }
        });

        callPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sureBook.setTextColor(context.getResources().getColor(R.color.schedule_book_info_font));
                sureBook.setBackground(context.getResources().getDrawable(R.drawable.circle_white_bg_gray_bound_0stroke));
                callPhone.setTextColor(context.getResources().getColor(R.color.white));
                callPhone.setBackground(context.getResources().getDrawable(R.drawable.circle_white_bg_gray_bound_0stroke_select));
                cancleBook.setTextColor(context.getResources().getColor(R.color.schedule_book_info_font));
                cancleBook.setBackground(context.getResources().getDrawable(R.drawable.circle_white_bg_gray_bound_0stroke));
            }
        });

        cancleBook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sureBook.setTextColor(context.getResources().getColor(R.color.schedule_book_info_font));
                sureBook.setBackground(context.getResources().getDrawable(R.drawable.circle_white_bg_gray_bound_0stroke));
                callPhone.setTextColor(context.getResources().getColor(R.color.schedule_book_info_font));
                callPhone.setBackground(context.getResources().getDrawable(R.drawable.circle_white_bg_gray_bound_0stroke));
                cancleBook.setTextColor(context.getResources().getColor(R.color.white));
                cancleBook.setBackground(context.getResources().getDrawable(R.drawable.circle_white_bg_gray_bound_0stroke_select));
            }
        });

    }
    private AlertDialog notifyDialog;
    private void showNoticy(int position) {
        int posi = position;
        notifyDialog = new AlertDialog.Builder(MainActivity.getInstace()).create();
        notifyDialog.show();
        Window window = notifyDialog.getWindow();
        // *** 主要就是在这里实现这种效果的.
        // 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
        window.setContentView(R.layout.info_dialog);
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);// 弹出键盘
        TextView info = (TextView) window.findViewById(R.id.call_state);
        RelativeLayout sureBtn = (RelativeLayout) window.findViewById(R.id.sure_btn_layout);
        RelativeLayout cloWin = (RelativeLayout) window.findViewById(R.id.close_win);

        sureBtn.setTag(posi);
        sureBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                notifyDialog.dismiss();
                int position = (int) v.getTag();
                Intent intent = new Intent(Intent.ACTION_CALL, // 创建一个意图
                        Uri.parse("tel:"
                                + bookData.getData().get(position).getCust_mobile()));
                MainActivity.getInstace().startActivity(intent);// 开始意图(及拨打电话)
            }
        });

        cloWin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                notifyDialog.dismiss();
            }
        });
    }
}
